package com.wangxinenpu.springbootdemo.controller;

import com.wangxinenpu.springbootdemo.dataobject.vo.process.ProcessDefinitionDetailVO;
import com.wangxinenpu.springbootdemo.dataobject.vo.process.ProcessInstanceVO;
import com.wangxinenpu.springbootdemo.dataobject.vo.task.TaskVO;
import org.activiti.api.task.model.builders.TaskPayloadBuilder;
import org.activiti.api.task.runtime.TaskRuntime;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/task")
public class TaskController {
    private final TaskService taskService;

    private final TaskRuntime taskRuntime;

    @Autowired
    public TaskController(TaskService taskService,TaskRuntime taskRuntime) {
        this.taskService = taskService;
        this.taskRuntime=taskRuntime;
    }

    @GetMapping("/{processInstanceId}")
    public List<TaskVO> getTasks(@PathVariable String processInstanceId) {
        List<Task> tasks=taskService.createTaskQuery().processInstanceId(processInstanceId).list();
        List<TaskVO> taskVOS=new ArrayList<>();
        for (Task task:tasks){
            TaskVO taskVO=new TaskVO();
            BeanUtils.copyProperties(task,taskVO);
            taskVOS.add(taskVO);
        }
        return taskVOS;
    }

    @GetMapping("complete")
    public void completeTask(@RequestParam("taskId") String taskId) {
        taskService.complete(taskId);
    }


    @GetMapping("claimTask")
    public void claimTask(String taskId) {
        taskRuntime.claim(TaskPayloadBuilder.claim().withTaskId(taskId).build());
    }
}
